﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace CletcWeb
{
    public partial class bbs_mulu_view : System.Web.UI.Page
    {
        protected string menu2_id, title_id;
        protected string titlename;

        protected void Page_Load(object sender, System.EventArgs e)
        {
            //if (!IsPostBack)
            //    ssConfig();

            Conn conn = new Conn();
            title_id = Request.Params["title_id"];
            menu2_id = Request.Params["menu2_id"];

            string uname1 = Session["username1"].ToString();
            if (!IsPostBack)
            {
                //Conn cc = new Conn();
                //string sql00 = "select chakan from xinxi where chakan like '%" + uname1 + ",%' and title_id=" + title_id;
                //if (cc.sqlFirst(sql00) == "0")
                //{
                //    string sql0 = "update xinxi set chakan=chakan + '" + uname1 + ",' where title_id=" + title_id;
                //    cc.Conn_Insert(sql0);
                //}
                //cc.Dispose();
            }
            string SelectSql = "select * from bbs where f_topic like '%"+menu2_id+"%' and  f_pos='" + title_id + "' or title_id=" + title_id + "order by f_date";

            DataSet ObjDataSet = conn.Conn_Send(SelectSql);

            string imgHead, imgTail;
            imgHead = "<img src=uploadimage" + "/";
            imgTail = "></img><br><br>";
            //string sfname = Session["sfname"].ToString();

            //if ((sfname == "系统管理员") | (sfname == "园管理员") | (sfname == "园长") | (sfname == "副园长") | (sfname == "园长助理") | (sfname == "主班老师") | (sfname == "配班老师") | (sfname == "生活老师") | (sfname == "年级组长"))
            //{
                //tbchakan.Visible = true;
                //lblchakan.Visible = true;
                //tbchakan.Text = conn.sqlFirst("select chakan from xinxi where title_id=" + title_id);
            //}

            DataGrid2.DataSource = ObjDataSet.Tables["a"].DefaultView;
            DataGrid2.Visible = true;
            DataGrid2.DataBind();

            for (int i = 0; i < DataGrid2.Items.Count; i++)
            {
                //				TextBox tbchakan=(TextBox)DataGrid2.Items[i].FindControl("tbchakan"); //查看贴子人员名单
                Label editer = (Label)DataGrid2.Items[i].FindControl("editer");
                Label writerLb = (Label)DataGrid2.Items[i].FindControl("writerLb");
                Label del2 = (Label)DataGrid2.Items[i].FindControl("del2");
                Label LaName1 = (Label)DataGrid2.Items[i].FindControl("LaName1");
                Label Label9 = (Label)DataGrid2.Items[i].FindControl("Label9");	//帖子内容
                Label Label6 = (Label)DataGrid2.Items[i].FindControl("Label6");	//帖子标题
                Label Updatetime_bbs = (Label)DataGrid2.Items[i].FindControl("Updatetime_bbs");	//修改时间
                Label Label8 = (Label)DataGrid2.Items[i].FindControl("Label8");	//创建时间
                Label lahuifu = (Label)DataGrid2.Items[i].FindControl("lahuifu");	//回复、创建小节
                Label lalayer = (Label)DataGrid2.Items[i].FindControl("lalayer");	//层级

                //层级=1表示为目录的章，只有章的才显示回复“新建一节”
                if (lalayer.Text == "1")
                    lahuifu.Visible = true;
                else
                    lahuifu.Visible = false;

                //修改时间、创建时间如果相同，则不显示修改时间
                if (Label8.Text == Updatetime_bbs.Text)
                    Updatetime_bbs.Visible = false;
                else
                {
                    Updatetime_bbs.Visible = true;
                    Updatetime_bbs.Text = "<br>此文章修改于：" + Updatetime_bbs.Text;
                }

                //帖子的标题、内容用HTML方式显示
                Label9.Text = Server.HtmlDecode(Label9.Text);
                Label6.Text = Server.HtmlDecode(Label6.Text);

                //删除、编辑权限
                string role = Session["sRole"].ToString();
                string uname = conn.sqlFirst("select f_writer from bbs where title_id=" + title_id);
                if ((role == "管理员") | (uname == Session["username"].ToString()) | (Session["sconfirm"].ToString() == "审核未通过") | (Session["sconfirm"].ToString() == "新建"))
                {
                    editer.Visible = true;
                    del2.Visible = true;
                }

                Label t2 = (Label)DataGrid2.Items[i].FindControl("t2");
                //查找图片
                string imgSql = "select f_filename,f_remark,flag,f_truename,f_width,f_height from ImgUpload where title_id=" + t2.Text;
                SqlConnection MyConnection = new SqlConnection(conn.strProvider);
                SqlCommand myCommand = new SqlCommand(imgSql, MyConnection);
                MyConnection.Open();
                SqlDataReader reader2 = myCommand.ExecuteReader();
                //图片个数
                string imgCountSql = "select count(*) from ImgUpload where title_id=" + title_id;
                SqlConnection MyConnection3 = new SqlConnection(conn.strProvider);
                SqlCommand myCommand3 = new SqlCommand(imgCountSql, MyConnection3);
                MyConnection3.Open();
                SqlDataReader reader3 = myCommand3.ExecuteReader();
                reader3.Read();
                int imgCount = (Int32)reader3.GetValue(0);

                Label Label10 = (Label)DataGrid2.Items[i].FindControl("Label10");		//图片
                Label10.Text = null;
                Label LaDoc2 = (Label)DataGrid2.Items[i].FindControl("LaDoc2");//附件
                string path = Server.MapPath("uploadimage") + "\\";//文件的路径
                int count = 1;
                while (reader2.Read())
                {
                    if (reader2.GetValue(2).ToString() == "1")
                    {
                        if (Int32.Parse(reader2.GetString(4).ToString()) > 600)
                            imgHead = "<img width=600 src=uploadimage" + "/";
                        Label10.Text += "<br><br>" + imgHead + reader2.GetValue(0).ToString() + imgTail + "<font size=2>" + reader2.GetValue(1).ToString() + "</font>";
                    }
                    else if (reader2.GetValue(2).ToString() == "2")
                    {
                        if (reader2.GetValue(1).ToString() != "")
                        {
                            LaDoc2.Text += "<object classid='clsid:D27CDB6E-AE6D-11CF-96B8-444553540000' id='obj1' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0' border='0' width='530' height='400'>" +
                            "<param name='movie' value='" + "uploadimage/" + reader2.GetValue(0).ToString() + "'>" +
                            "<param name='quality' value='High'>" +
                            "<embed src='" + "uploadimage/" + reader2.GetValue(0).ToString() + "' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' name='obj1' width='530' height='400'></object>";
                            //LaDoc2.Text += "<br><font size=2>附件" + count + "：<a href='" + "uploadimage/" + reader2.GetValue(0).ToString() + "' target='blank'>" + reader2.GetValue(3).ToString() + "</a>&nbsp;（" + reader2.GetValue(1).ToString() + "）</font><br>";
                        }
                        else
                            LaDoc2.Text += "<object classid='clsid:D27CDB6E-AE6D-11CF-96B8-444553540000' id='obj1' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0' border='0' width='530' height='400'>" +
                            "<param name='movie' value='" + "uploadimage/" + reader2.GetValue(0).ToString() + "'>" +
                            "<param name='quality' value='High'>" +
                            "<embed src='" + "uploadimage/" + reader2.GetValue(0).ToString() + "' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' name='obj1' width='530' height='400'></object>";
                        //LaDoc2.Text += "<br><font size=2>附件" + count + "：<a href='" + "uploadimage/" + reader2.GetValue(0).ToString() + "' target='blank'>" + reader2.GetValue(3).ToString() + "</a></font><br>";
                        count = count + 1;
                    }
                }
                reader2.Close();
                reader3.Close();
                MyConnection3.Close();
                MyConnection.Close();
            }

            if (!Page.IsPostBack)
            {
                //标题表的阅读数＋1
                string UpSql = "UPDATE xinxi SET f_read = f_read + 1 WHERE title_id =" + title_id;
                conn.Conn_Insert(UpSql);
            }
            conn.Dispose();
        }
    }
}